﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Threading;
using LitJson;
using System.Web.Security;
using WxPay;
using BLL;
using Model;
using NS.Utilities;
using NS.BLL.Systems;
using WechatUtility;


public partial class wechat_JsApiPayPage : System.Web.UI.Page
{
    public static string wxJsApiParam { get; set; } //H5调起JS API参数
    protected void Page_Load(object sender, EventArgs e)
    {
        Log.Info(this.GetType().ToString(), "page load");
        if (!IsPostBack)
        {
            JsApiPay jsApiPay = new JsApiPay(this);
            CookieHelper.SetObject("orderid", Request.QueryString["OrderID"].ToString());
            string openid = CookieHelper.GetValue("openid");
            Order O = OrderService.GetOrderByOrderID(long.Parse(Request.QueryString["OrderID"].ToString()));
            string total_fee = (Convert.ToInt32(O.Pay * 100)).ToString();
            if (WebConfigHelper.GetAppSet("Debug") == "1")
            {
                total_fee = "1";
            }
            string body = O.CarNumber;
            //string body = O.CarNumber + O.Project;
           
            string attach = "0";
            //检测是否给当前页面传递了相关参数
            if (string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(total_fee) || string.IsNullOrEmpty(attach))
            {
                Response.Write("<span style='color:#000000;font-size:20px'>" + "获取参数出错,请返回重试" + "</span>");
                Log.Error(this.GetType().ToString(), "获取参数出错,请返回重试");
                //submit.Visible = false;
                return;
            }

            //若传递了相关参数，则调统一下单接口，获得后续相关接口的入口参数
            //JsApiPay jsApiPay = new JsApiPay(this);
            jsApiPay.openid = openid;
            jsApiPay.total_fee = int.Parse(total_fee);
            jsApiPay.body = body;
            jsApiPay.attach = attach;
            jsApiPay.out_trade_no = WxPayApi.GenerateOutTradeNo();
            //JSAPI支付预处理

            try
            {
                WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();
                Log.Debug(this.GetType().ToString(), "调用统一下单，获得下单结果成功");
                UtilityService.UpdateFieldByPK("Order", "TradeNo", new Object[] { jsApiPay.out_trade_no }, "OrderID", Request.QueryString["OrderID"].ToString());
                Log.Debug(this.GetType().ToString(), "数据库更新成功");
                wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数                    
                Log.Debug(this.GetType().ToString(), "wxJsApiParam : " + wxJsApiParam);
                orderNo.InnerHtml = O.OrderNo;
                payCount.InnerHtml = Convert.ToDouble(O.Pay).ToString("0.00");
                //Response.Write("<span style='font-size:20px'>订单详情：</span><br/>");
                //Response.Write("<span style='font-size:20px'>商品名称：" + body + "</span><br/>");
                //Response.Write("<span style='font-size:20px'>商品描述：" + attach + "</span><br/>");
                //Response.Write("<span style='font-size:20px'>商户订单号：" + jsApiPay.out_trade_no + "</span><br/>");
                //Response.Write("<span style='font-size:20px'>订单总金额(分)：" + total_fee + "</span><br/>");

            }
            catch (Exception ex)
            {
                //Response.Write("<span style='color:#000000;font-size:20px'>" + jsApiPay.GetUnifiedOrderResult() + "网络不给力，请返回重试" + "</span>");
                Response.Write("<span style='color:#000000;font-size:20px'>" + "网络不给力，请点击返回重试" + "</span>");
                //submit.Visible = false;
            }
        }
    }
}